//
//  MessageVoiceBubbleView.m
//  BaiTengIM
//
//  Created by awh on 2019/6/30.
//  Copyright © 2019 awh. All rights reserved.
//

#import "MessageVoiceBubbleView.h"
#import "VoiceMessageModel.h"


#define kEMMsgAudioMinWidth 30
#define kEMMsgAudioMaxWidth 120


@implementation MessageVoiceBubbleView

- (instancetype)initWithDirection:(MessageDirection)messageDirection type:(MessageType)messageType {
    self = [super initWithDirection:messageDirection type:messageType];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

#pragma mark - Subviews

- (void)setupSubviews {
    [self setupBubbleBackgroundImage];
    self.imgView = [[UIImageView alloc] init];
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;
    self.imgView.clipsToBounds = YES;
    [self addSubview:self.imgView];
    self.imgView.animationDuration = 1.0;
    [self addSubview:self.imgView];
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.top.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.width.height.equalTo(@30);
    }];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.font = [UIFont systemFontOfSize:16];
    self.textLabel.numberOfLines = 0;
    [self addSubview:self.textLabel];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self.imgView.mas_left).offset(-10);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    if (self.messageDirection == MessageDirectionSend) {
        self.imgView.image = [UIImage imageNamed:@"msg_send_audio"];
        self.imgView.animationImages = @[[UIImage imageNamed:@"msg_send_audio01"], [UIImage imageNamed:@"msg_send_audio02"], [UIImage imageNamed:@"msg_send_audio"]];
        self.textLabel.textColor = [UIColor blackColor];
    } else {
        self.imgView.image = [UIImage imageNamed:@"msg_recv_audio"];
        self.imgView.animationImages = @[[UIImage imageNamed:@"msg_recv_audio01"], [UIImage imageNamed:@"msg_recv_audio02"], [UIImage imageNamed:@"msg_recv_audio"]];
        self.textLabel.textColor = [UIColor whiteColor];
    }
}

#pragma mark - Setter

- (void)setModel:(BTMessageModel *)model {
    if (model.msgtype == MessageTypeVoice) {
        VoiceMessageModel *voiceModel = (VoiceMessageModel *)model;
        if (voiceModel.isPlaying) {
            [self.imgView startAnimating];
        } else {
            [self.imgView stopAnimating];
        }
        self.textLabel.textColor = model.messageDirection == MessageDirectionSend ? [UIColor whiteColor] : [UIColor blackColor];
        
        
//        NSInteger duration = [[voiceModel.content componentsSeparatedByString:@","][0] integerValue];
        NSInteger duration =voiceModel.duration;
//        self.textLabel.text = [NSString stringWithFormat:@"%d\"",duration];
        self.textLabel.text = [NSString stringWithFormat:@"%ld\"",voiceModel.duration];
        CGFloat width = kEMMsgAudioMinWidth * duration / 10.0;
        if (width > kEMMsgAudioMaxWidth) {
            width = kEMMsgAudioMaxWidth;
        } else if (width < kEMMsgAudioMinWidth) {
            width = kEMMsgAudioMinWidth;
        }
        [self.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
        }];
    }
}
@end
